layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 商品评价管理
     */
    var TaoGoodsEvaluate = {
        tableId: "taoGoodsEvaluateTable"
    };

    /**
     * 初始化表格的列
     */
    TaoGoodsEvaluate.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'goodNames', sort: true, title: '商品名称'},
            {
                field: 'pic', sort: true, title: '评价图片', templet: function (d) {
                    if (d.pic != null && d.pic != "") {
                        var html = "";
                        var imgs = d.pic.split(",");
                        for (var i = 0; i < imgs.length; i++) {
                            if(imgs[i]){
                                html = html + '<img alt="image" id="images" style="width: 50px;height: 50px;display: inline" class="img-responsive" onclick="show_img(\'' + imgs[i] + '\')"  src="' + imgs[i] + '">'
                            }
                        }
                        return html;
                    } else {
                        return '<span>图片未上传</span>';
                    }
                }
            },
            {
                field: 'videoUrl', sort: true, title: '视频', templet: function (d) {
                    if (d.videoUrl != null && d.videoUrl != "") {
                        var html = "";
                        var imgs = d.videoUrl.split(",");
                        for (var i = 0; i < imgs.length; i++) {
                            if(imgs[i]){
                                html = html + '<video style="width: 60%; height: 50%;" controls preload="auto">' +
                                    '<source src="' + imgs[i] + '" type="video/mp4" controls autoplay />' +
                                    '</video>'
                            }
                        }
                        return html;
                    } else {
                        return '<span>图片未上传</span>';
                    }
                }
            },
            {field: 'content', sort: true, title: '评价内容'},
            {field: 'evaluateStars', sort: true, title: '整体评价'},
            {field: 'serviceAttitude', sort: true, title: '服务态度评价'},
            {field: 'logisticsService', sort: true, title: '物流服务评价'},
            {field: 'weixinName', sort: true, title: '评价人'},
            {field: 'time', sort: true, title: '评价时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    TaoGoodsEvaluate.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(TaoGoodsEvaluate.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    TaoGoodsEvaluate.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/taoGoodsEvaluate/add';
    };

    /**
     * 导出excel按钮
     */
    TaoGoodsEvaluate.exportExcel = function () {
        var checkRows = table.checkStatus(TaoGoodsEvaluate.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    TaoGoodsEvaluate.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/taoGoodsEvaluate/edit?=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    TaoGoodsEvaluate.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/taoGoodsEvaluate/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(TaoGoodsEvaluate.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TaoGoodsEvaluate.tableId,
        url: Feng.ctxPath + '/taoGoodsEvaluate/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: TaoGoodsEvaluate.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TaoGoodsEvaluate.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        TaoGoodsEvaluate.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        TaoGoodsEvaluate.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + TaoGoodsEvaluate.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            TaoGoodsEvaluate.openEditDlg(data);
        } else if (layEvent === 'delete') {
            TaoGoodsEvaluate.onDeleteItem(data);
        }
    });


});




/**
 * 点击查看图片
 */
function show_img(t) {
    //页面层
    layer.open({
        type: 1,
        area: ['50%', '50%'], //宽高
        title: false,
        shadeClose: true, //开启遮罩关闭
        end: function (index, layero) {
            return false;
        },
        content: "<img style='width: 100%' src='" + t + "'/>"
    });
}





